package com.lanmei.group.weixin.controller.admin;

import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;

import com.jfinal.aop.Clear;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.lanmei.group.weixin.interceptor.ManagerInterceptor;
import com.lanmei.group.weixin.model.ManagerInfo;
import com.lanmei.group.weixin.model.SiteInfo;
import com.lanmei.group.weixin.utils.ShiroUtil;
import com.lanmei.group.weixin.utils.StringUtil;
@Clear(ManagerInterceptor.class)
@RequiresRoles(value = {"admin" })
public class ManagerInfoController extends Controller {
	
	public void list(){
		Page<ManagerInfo> page = new ManagerInfo().p(1, 10);
		List<ManagerInfo> list =page.getList();
		for (int i = 0; i < list.size(); i++) {
			ManagerInfo manager = list.get(i);
			SiteInfo site = SiteInfo.dao.findById(manager.getSiteId());
			if(null!=site){
				manager.setSiteName(site.getSiteName());
			}
		}
		setAttr("page", page);
		render("list.jsp");
	}
	public void toAdd(){
		Page<SiteInfo> page = new SiteInfo().paginat(1, 20);
		setAttr("page", page);
		render("add.jsp");
	}
	public void add(){
		ManagerInfo manager = getModel(ManagerInfo.class, "manager");
		ManagerInfo temp = new ManagerInfo().findManagerInfoByLoginName(manager.getLoginName());
		if(null!=temp){
			setAttr("msg", "该登录用户名已经被注册！");
			Page<SiteInfo> page = new SiteInfo().paginat(1, 20);
			setAttr("page", page);
			render("add.jsp");
			return;
		}
		manager.setPwd(StringUtil.MD5(manager.getPwd()));
		manager.setCreateTime(new Date());
		manager.setCreateUser(ShiroUtil.getLoginName());
		manager.save();
		this.redirect("/admin/managerinfo/list");
	}
	public void toEdit(){
		int managerId = getParaToInt("managerId");
		ManagerInfo manager = ManagerInfo.dao.findById(managerId);
		Page<SiteInfo> page = new SiteInfo().paginat(1, 20);
		setAttr("page", page);
		setAttr("manager", manager);
		render("edit.jsp");
	}
	public void update(){
		ManagerInfo manager = getModel(ManagerInfo.class, "manager");
		ManagerInfo temp = new ManagerInfo().findManagerInfoByLoginName(manager.getLoginName());
		if(null!=temp&&manager.getManagerId()!=temp.getManagerId()){
			setAttr("msg", "该登录用户名已经存在！");
			Page<SiteInfo> page = new SiteInfo().paginat(1, 20);
			setAttr("page", page);
			ManagerInfo manager1 = ManagerInfo.dao.findById(manager.getManagerId());
			setAttr("manager", manager1);
			render("add.jsp");
			return;
		}
		manager.setUpdateTime(new Date());
		manager.setUpdateUser(ShiroUtil.getLoginName());
		manager.update();
		this.redirect("/admin/managerinfo/list");
	}
	public void delete(){
		int managerId = getParaToInt("managerId");
		ManagerInfo manager = ManagerInfo.dao.findById(managerId);
		manager.delete();
		this.redirect("/admin/managerinfo/list");
	}
}
